/*
Joseph Ratway
Garrett Brown
Bret Parker
*/

//imports
import java.io.*;
import java.util.*;
import java.net.*;

public class Parser
{
	String command;
	int undoCount=0;
	
	
	Parser(){};

	int ParseLine(ReversiGame rgame, Socket socket)
	{
		command = rgame.command;
		
		if(checkCommandSyntax(command)==true)
		{
			if(command.compareTo("REDO")==0)
			{
				rgame.RedoMove();
				if(rgame.displayBoard==true)
				{
						rgame.printBoardToClient(socket);
				}
				rgame.currentBoard.printBoard();
				return 3;
			}
			else if(command.compareTo("UNDO")==0)
			{
				rgame.UndoMove();
				if(rgame.displayBoard==true)
					{
						rgame.printBoardToClient(socket);
					}
				undoCount++;
				rgame.currentBoard.printBoard();
				return 4;
			}
			else if(command.compareTo("SHOW_NEXT_POS")==0)
			{
			
				if(rgame.startingColor != 0 && rgame.Difficulty !="")
				{
					rgame.currentBoard.isValidMove(rgame.currentBoard.currentBoard);
					rgame.currentBoard.printBoard();
					rgame.printBoardToClient(socket);
					rgame.startGame(socket);
					return 1;
				}
				else
				{
					rgame.out.println("need to set color and difficulty");
				}
			}
			else if(command.compareTo("EXIT")==0)
			{
				System.exit(0);
				return 1;
			}
			else if(command.compareTo("DISPLAY_ON")==0)
			{
				rgame.displayBoard=true;
				rgame.printBoardToClient(socket);
				rgame.startGame(socket);
				//display on
				return 1;
			}
			else if(command.compareTo("DISPLAY_OFF")==0)
			{
				//display off
				rgame.displayBoard=false;
				rgame.startGame(socket);
				return 1;
			}
			else if(command.compareTo("EASY")==0)
			{
				if(rgame.startingColor != 0)
				{
					if(rgame.startingColor==2)
					{
						rgame.easyAI();
					}
					
					rgame.Difficulty="EASY";
					rgame.out.println("'EASY'");
					rgame.setDiff=true;
					if(rgame.displayBoard==true)
					{
						rgame.printBoardToClient(socket);
					}
				}
				else
				{
					rgame.out.println("Need to choose Color first");
				}
				rgame.startGame(socket);
			}
			else if(command.compareTo("NOVICE")==0)
			{
				if(rgame.startingColor != 0)
				{
					if(rgame.startingColor==2)
					{
						rgame.noviceAI();
					}
					
					rgame.Difficulty="NOVICE";
					rgame.out.println("'NOVICE'");
					rgame.setDiff=true;
					if(rgame.displayBoard==true)
					{
						rgame.printBoardToClient(socket);
					}
				}
				else
				{
					rgame.out.println("Need to choose Color first");
				}
			}
			else if(command.compareTo("MEDIUM")==0)
			{
				if(rgame.startingColor != 0)
				{
					if(rgame.startingColor==2)
					{
						rgame.mediumAI();
					}
				
					rgame.Difficulty="MEDIUM";
					rgame.out.println("'MEDIUM'");
					rgame.setDiff=true;
					if(rgame.displayBoard==true)
					{
						rgame.printBoardToClient(socket);
					}
				}
				else
				{
					rgame.out.println("Need to choose Color first");
				}
			}
			else if(command.compareTo("HARD")==0)
			{
				if(rgame.startingColor != 0)
				{
					if(rgame.startingColor==2)
					{
						rgame.hardAI();
					}
				
					rgame.Difficulty="HARD";
					rgame.out.println("'HARD'");
					rgame.setDiff=true;
					if(rgame.displayBoard==true)
					{
						rgame.printBoardToClient(socket);
					}
				}
				else
				{
					rgame.out.println("Need to choose Color first");
				}
			}
			else if(command.compareTo("RAND")==0)
			{
				if(rgame.startingColor!=0 && rgame.Difficulty!="")
				{
					rgame.currentBoard.random(rgame.currentBoard.currentBoard);
					if(rgame.displayBoard==true)
					{
						rgame.printBoardToClient(socket);
					}
					rgame.currentBoard.printBoard();
				}
				else
				{
					rgame.out.println("need to set color and difficulty");
				}
			}
			else if(command.compareTo("BLACK")==0)
			{
				//black
				rgame.out.println("'BLACK'");
				rgame.startingColor=1;
				return 1;
			}
			else if(command.compareTo("WHITE")==0)
			{
				rgame.startingColor=2;
				rgame.out.println("'WHITE'");
				return 1;
			}
			else if(command.compareTo("BATTLE")==0)
			{
					rgame.out.println("Which AI will battle each other.");
					rgame.out.println("Black will be:");
					
					String blackAI="";
					String whiteAI="";
					
					try
					{
						blackAI = rgame.in.readLine();
						rgame.out.println("White will be:");
						whiteAI = rgame.in.readLine();
					}
					catch(IOException ex)
					{
						ex.printStackTrace();
					}
				while(rgame.currentBoard.endCondition>0)
				{
					if(blackAI.compareTo("NOVICE")==0)
					{
							rgame.noviceAI();
					}
					else if(blackAI.compareTo("EASY")==0)
					{
							rgame.easyAI();
					}
					else if(blackAI.compareTo("MEDIUM")==0)
					{
							rgame.mediumAI();
					}
					else if(blackAI.compareTo("HARD")==0)
					{
							rgame.hardAI();
					}
					
					if(rgame.displayBoard==true)
						rgame.printBoardToClient(socket);
					rgame.currentBoard.printBoard();
					
					if(whiteAI.compareTo("NOVICE")==0)
					{
							rgame.noviceAI();
					}
					else if(whiteAI.compareTo("EASY")==0)
					{
							rgame.easyAI();
					}
					else if(whiteAI.compareTo("MEDIUM")==0)
					{
							rgame.mediumAI();
					}
					else if(whiteAI.compareTo("HARD")==0)
					{
							rgame.hardAI();
					}
					if(rgame.displayBoard==true)
						rgame.printBoardToClient(socket);
					rgame.currentBoard.printBoard();
				}				
			}
			else
			{
				rgame.out.println("Error");
			}
		}
		
		else if(checkMoveSyntax(command)==true)
		{ 
			int i=1;
			while(undoCount>=0)
				{
					rgame.boardStates.remove(rgame.boardStates.size()-i);
					undoCount--;
					i++;
				}
			undoCount=0;
	
			if(rgame.Difficulty.compareTo("EASY")==0 && rgame.setDiff==true)
			{
				if(rgame.displayBoard==true)
				{
					rgame.printBoardToClient(socket);
				}
				System.out.println("Selecting Easy");
				//rgame.setCommand(socket);
				doMove(rgame, rgame.command, socket);
				rgame.currentBoard.printBoard();
				rgame.easyAI();
				rgame.currentBoard.printBoard();
				if(rgame.displayBoard==true)
				{
					rgame.printBoardToClient(socket);
				}
				rgame.startGame(socket);
			}
			else if(rgame.Difficulty.compareTo("NOVICE")==0 && rgame.setDiff==true)
			{
				//Random
				if(rgame.displayBoard==true)
				{
					rgame.printBoardToClient(socket);
				}
				System.out.println("Selecting ");
				//rgame.setCommand(socket);
				doMove(rgame, rgame.command, socket);
				rgame.currentBoard.printBoard();
				rgame.noviceAI();
				rgame.currentBoard.printBoard();
				if(rgame.displayBoard==true)
				{
					rgame.printBoardToClient(socket);
				}
				rgame.startGame(socket);
			}
			else if(rgame.Difficulty.compareTo("MEDIUM")==0 && rgame.setDiff==true)
			{
				//medium
					if(rgame.displayBoard==true)
				{
					rgame.printBoardToClient(socket);
				}
				System.out.println("Selecting Medium");
				//rgame.setCommand(socket);
				doMove(rgame, rgame.command, socket);
				rgame.currentBoard.printBoard();
				rgame.mediumAI();
				rgame.currentBoard.printBoard();
				if(rgame.displayBoard==true)
				{
					rgame.printBoardToClient(socket);
				}
				rgame.startGame(socket);
			}
			else if(rgame.Difficulty.compareTo("HARD")==0 && rgame.setDiff==true)
			{
				//hard
				if(rgame.displayBoard==true)
				{
					rgame.printBoardToClient(socket);
				}
				System.out.println("Selecting Novice");
				//rgame.setCommand(socket);
				doMove(rgame, rgame.command, socket);
				rgame.currentBoard.printBoard();
				rgame.hardAI();
				rgame.currentBoard.printBoard();
				if(rgame.displayBoard==true)
				{
					rgame.printBoardToClient(socket);
				}
				rgame.startGame(socket);
			}
			else
			{
				rgame.out.println("Error");
				System.out.println("Error");
			}
		}
		else
	    {
			System.out.println("Incorrect Input, Try Again");
			rgame.setCommand(socket);
			ParseLine(rgame, socket);
		}
		
		return 0;
	}
	
	boolean checkCommandSyntax(String str)
	{	
		if( (str.compareTo("BATTLE")==0)||(str.compareTo("RAND")==0) ||(str.compareTo("EASY")==0) ||(str.compareTo("NOVICE")==0) ||(str.compareTo("REDO")==0) ||(str.compareTo("UNDO")==0) || (str.compareTo("SHOW_NEXT_POS")==0) || (str.compareTo("EXIT")==0)|| (str.compareTo("DISPLAY_ON")==0) || (str.compareTo("DISPLAY_OFF")==0)|| (str.compareTo("EASY")==0) || (str.compareTo("MEDIUM")==0)|| (str.compareTo("HARD")==0) || (str.compareTo("BLACK")==0) || (str.compareTo("WHITE")==0)||(str.compareTo("ran")==0)) 
			return true;
		else
			return false;			
	}
	
	boolean checkMoveSyntax(String str)
	{
		char[] charArray;
		charArray = str.toCharArray();
        if(str.length() < 2)
        {
            return false;
        }
	
		if((charArray[0] >= 'a' && charArray[0] <= 'z'))
		{
			if(Character.isDigit(charArray[1]) == true )
				return true;
			else
				return false;
		}
		else
			return false;	
	}
	
	
	void doMove(ReversiGame rgame, String command, Socket socket)
	{
		if(checkMoveSyntax(command)==true)
		{ 
			if(rgame.currentBoard.endCondition>=0)
			{
				if(rgame.currentBoard.makeMove(rgame.currentBoard.currentBoard, rgame.currentBoard.player, command)==-1)
				{
					rgame.out.println("Not a Valid Move (hint: Try Typing 'SHOW_NEXT_POS')");
					rgame.startGame(socket);	
				}
				else
				{
					rgame.currentBoard.makeMove(rgame.currentBoard.currentBoard, rgame.currentBoard.player, command);
				}
			}
		}
	}
	
}
	
	
	
